package com.example.myapplication1;


import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import androidx.annotation.NonNull;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateListActivity extends ListActivity implements AdapterView.OnItemClickListener {
    private static final String TAG ="zlhhhh";
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       /* String[]list_data={"one","two","three","four"};
        //构造适配器
        List<String> list1=new ArrayList<String>();
        for(int i=1;i<100;i++){
            list1.add("item"+i);
        }
        ListAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list_data);
        //绑定
*/        handler=new Handler(Looper.getMainLooper()){
            public void handleMessage(@NonNull Message msg){
                if(msg.what==9) {
                    List<String> list3 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(
                            RateListActivity.this,
                            android.R.layout.simple_list_item_1, list3);
                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };
       getListView().setOnItemClickListener(this);
        Thread t = new Thread(()->{
            List<String> list2 = new ArrayList<String>();
            Log.i(TAG, "run;子线程run().....");
            URL url = null;
            Bundle retbd1 = new Bundle();
            try {
                Document doc = Jsoup.connect("https://www.huilvbiao.com/bank/spdb").get();
                Log.i(TAG,"run:title ="+doc.title());
                Elements tbodys = doc.getElementsByTag("tbody");
                Element tbody = tbodys.get(0);
                Log.i(TAG,"run:table2="+tbody);
                Elements trs = tbody.getElementsByTag("tr");
                for(Element tr:trs){
                    Element currencyName = tr.selectFirst("th.table-coin a.coin span");
                    Elements tds = tr.select("td");
                    Element td2 = tds.get(0);
                    Element td3 = tds.get(1);
                    String str1 = currencyName.text();
                    String str2 = td2.text();
                    String str3 = td3.text();
                    float buyRate = Float.parseFloat(str2);
                    float soldRate = Float.parseFloat(str3);
                    float rate = (buyRate + soldRate) / 2.0f;
                    String rateStr = String.valueOf(rate);
                    list2.add(str1+"==>"+rateStr);
                    Log.i(TAG,"run:"+str1+"==>"+rateStr);

                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }

            Log.i(TAG,"创建list");
            Message msg=handler.obtainMessage(9,list2);
            handler.sendMessage(msg);
        });
        t.start();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        HashMap<String, String> map=  (HashMap<String, String>) getListView().getItemAtPosition(position);
        String titleStr = map.get("ItemTitle");
        String priceStr = map.get("price");
        Log.i(TAG, "onItemClick: titleStr=" + titleStr);
        Log.i(TAG, "onItemClick: detailStr=" + priceStr);
        Log.i(TAG, "onItemClick: position="+position);
    }

}